<?php

/**
 * @Author: king
 * @Date:   2019-06-05 21:42:59
 * @Last Modified by:   king
 * @Last Modified time: 2020-08-07 20:46:26
 */

namespace App\Http\Controllers\Api\v3;

use App\Http\Controllers\Api\BaseController;
use App\Models\Paytype;
use App\Models\TOrders;
use App\Tools\Orders as OrdersLib;
use App\Tools\Astrolog\Astrolog;
use Illuminate\Http\Request;

/**
 * 充值列表
 * Class Astrolabe
 * @package use App\Tools\Astrolog\Astrolog
 */
class AppPayController extends BaseController
{

 public function list(Request $request)
    {
        $list = Paytype::where("type",'0')->where("deletime",0)->orderBy("ord")->select("id","price","type","num","ord")->get();
        return show(config('code.success'), '获取成功', $list);
    }
 /**
  * 创建充值订单
  * **/

 public function create(Request $request){
        $param  = $request->all();
        $gid = $param['gid'];
        $openid = $param['openid'] ?? '';
        $callback_url = $param['callback_url'] ?? '';
        $order_type = $param['order_type'] ?? '';
        if($gid == ''){
            return show(config('code.error'), '请选择充值产品', [], 403);
        }
        if($param['trade_type'] == ''){
            return show(config('code.error'), '请选择充值方式', [], 403);
        }
        // print_r($param);die;

        $user_id = $request->uid;
        $goodsInfo = Paytype::where("deletime",0)->find($gid);
        $data = [
            'uid'=> $request->uid,
            'gid' => $param['gid'],
            'order_id' => "P".createOrderNumber(7),
            'trade_type' => $param['trade_type'],
            'total_amount' => $goodsInfo['price'],
            'create_time' => time(),
        ];

        $TOrder = TOrders::create($data);
        $arr = (new OrdersLib())->unifyPayOrder(['trade_type' => $TOrder['trade_type'], 'total_amount' => $TOrder['total_amount'], 'pay_des' => '余额充值', 'order_id' => $TOrder['order_id'],'openid'=>$openid,'callback_url'=>$callback_url,'order_type'=>$order_type]);

        // 推送队列
        // $fortune_job_setting = config('fortune_job');
        // $delay = $fortune_job_setting['delay'];
        // // print_r($fortune_job_setting);die;
        // queuePush($fortune_job_setting['task'], $TOrder, $fortune_job_setting['queue'], $delay);

        return show(config('code.success'), '获取成功', $arr['data']);
    }


}
